package com.example.basic_ui_demo.view.news

import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.basic_ui_demo.NewsActivity
import com.example.basic_ui_demo.view_model.NewsViewModel
import com.lt.compose_views.banner.Banner
import com.lt.compose_views.banner.rememberBannerState
import com.lt.compose_views.pager_indicator.PagerIndicator

@Composable
fun BannerCompose(viewModel: NewsViewModel) {
    val bannerState = rememberBannerState()
    val bannerList = remember { viewModel.bannerList }
    val context = LocalContext.current

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp)
    ) {
        Banner(
            bannerList.size,
            Modifier,
            bannerState = bannerState,
            autoScrollTime = 2000,
            orientation = Orientation.Horizontal,
        ) {
            Box(
                modifier = Modifier.background(Color.LightGray)
            ) {
                //新闻图片
                Image(
                    painter = bannerPainter(picUrl = bannerList[index].picUrl),
                    contentDescription = "",
                    modifier = Modifier
                        .fillMaxSize()
                        .clickable {
                            val intent = Intent(context, NewsActivity::class.java)
                                .putExtra("URL", bannerList[index].url)
                            context.startActivity(intent)
                        }
                )
                //新闻描述
                Text(
                    text = bannerList[index].title,
                    modifier = Modifier
                        .align(Alignment.BottomCenter)
                        .padding(bottom = 20.dp)
                        .background(Color.Black.copy(alpha = 0.3f)),
                    color = Color.White,
                )
            }
        }

        //圆形指示器
        PagerIndicator(
            size = bannerList.size,
            offsetPercentWithSelectFlow = remember { bannerState.createChildOffsetPercentFlow() },
            selectIndexFlow = remember { bannerState.createCurrSelectIndexFlow() },
            indicatorItem = {
                Spacer(
                    modifier = Modifier
                        .size(10.dp)
                        .background(Color.Black, CircleShape)
                )
            },
            selectIndicatorItem = {
                Spacer(
                    modifier = Modifier
                        .size(10.dp)
                        .background(Color.Green, CircleShape)
                )
            },
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(10.dp),
            orientation = Orientation.Horizontal,
        )
    }
}